package org.reos;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.TreeMap;

/**
 * Created with IntelliJ IDEA.
 * User: cfrn76
 * Date: 26.12.13
 * Time: 18:17
 * To change this template use File | Settings | File Templates.
 */
public class ReLogPrinter {
    private TreeMap<Integer, String> stringMap;
    public ReLogPrinter(TreeMap<Integer, String> stringMap) {
        this.stringMap = stringMap;
    }

    public void processData(ArrayList<ReLogByte> data) {
        if (data == null || data.size() == 0) {
            return;
        }
        long ts = data.get(0).getTimestamp();
        int address = ((data.get(0).getByteValue()) << 8) | data.get(1).getByteValue();
        if (!stringMap.containsKey(address)) {
            return;
        }
        String logStr = "[" + ts + "] ReLogPrinter: " + stringMap.get(address);

        if (data.size() > 2) {
            // There are some parameters beside the string itself
            ArrayList<Integer> params = new ArrayList<Integer>();
            for (int i = 2; i < data.size(); i += 2) {
                params.add((data.get(i).getByteValue() << 8) | data.get(i + 1).getByteValue());
            }
            Formatter formatter = new Formatter(new StringBuilder());
            logStr = formatter.format(logStr, (Object[]) params.toArray()).toString();
        }
        System.out.println(logStr);
    }
}
